using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using Aga.Psad.Backend.Repositories;
using AutoMapper;
using Aga.Psad.Backend.Entities;
using Aga.Psad.Mvc.Models.TipoDocumento;

namespace Aga.Psad.Mvc.Controllers
{
    [Authorize(Roles = "Administrador")]
    public class TipoDocumentoController : ControllerBase
    {
        public TipoDocumentoRepository repository { get; private set; }

        public TipoDocumentoController()
        {
            repository = new TipoDocumentoRepository();

            InitializeMappers();
        }

        private void InitializeMappers()
        {
            Mapper.CreateMap<TipoDocumento, TipoDocumentoViewModel>();
            Mapper.CreateMap<TipoDocumentoViewModel, TipoDocumento>();
        }

        //
        // GET: /TipoDocumento/

        public ActionResult Index()
        {
            try
            {
                var tipos = repository.GetAll();

                var model = Mapper.Map<List<TipoDocumento>, List<TipoDocumentoViewModel>>(tipos);

                return View(model);

            }
            catch (Exception ex)
            {
                return ShowError(ex);
            }
        }

        //
        // GET: /TipoDocumento/Details/5

        public ActionResult Details(int id)
        {
            try
            {
                var tipo = repository.GetById(id);

                var model = Mapper.Map<TipoDocumento, TipoDocumentoViewModel>(tipo);

                return View(model);

            }
            catch (Exception ex)
            {
                return ShowError(ex);
            }
        }

        //
        // GET: /TipoDocumento/Create

        public ActionResult Create()
        {
            try
            {
                TipoDocumentoViewModel model = new TipoDocumentoViewModel();

                return View(model);
            }
            catch (Exception ex)
            {
                return ShowError(ex);
            }
        } 

        //
        // POST: /TipoDocumento/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                TipoDocumentoViewModel model = new TipoDocumentoViewModel();

                UpdateModel(model);

                if (!model.IsValid)
                {
                    foreach (var issue in model.GetRuleViolations())
                    {
                        ModelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
                    }

                    return View(model);
                }

                TipoDocumento entity = Mapper.Map<TipoDocumentoViewModel, TipoDocumento>(model);

                repository.Save(entity);

                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                return ShowError(ex);
            }
        }

        //
        // GET: /TipoDocumento/Edit/5
 
        public ActionResult Edit(int id)
        {
            try
            {
                TipoDocumento entity = repository.GetById(id);

                TipoDocumentoViewModel model = Mapper.Map<TipoDocumento, TipoDocumentoViewModel>(entity);

                return View(model);
            }
            catch (Exception ex)
            {
                return ShowError(ex);
            }
        }

        //
        // POST: /TipoDocumento/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                TipoDocumento entity = repository.GetById(id);
                TipoDocumentoViewModel model = Mapper.Map<TipoDocumento, TipoDocumentoViewModel>(entity);

                UpdateModel(model);

                if (!model.IsValid)
                {
                    foreach (var issue in model.GetRuleViolations())
                    {
                        ModelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
                    }

                    return View(model);
                }

                entity = Mapper.Map<TipoDocumentoViewModel, TipoDocumento>(model, entity);

                repository.Save(entity);

                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                return ShowError(ex);
            }
        }
    }
}
